﻿using EFFC.Frame.Net.Base.Data.Base;
using EFFC.Frame.Net.Module.Extend.EWRA.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EFFC.Extends.LinqDLR2SQL;
using EFFC.Frame.Net.Unit.DB;
using EFFC.Frame.Net.Base.Common;
using EFFC.Frame.Net.Module.Extend.EWRA.Attributes.Validation;
using ICPFrameLib.Business;
using ICPFrameLib.Attributes;

namespace XHRPMIS.Business.v1._0
{
    public class Role:MyRestLogic
    {
        [ActionAuth("Role")]
        [EWRARouteDesc("获取所有角色的列表")]
        [EWRAOutputDesc("返回结果", @"{
result:[数据集]
}")]
        public override List<object> get()
        {
            SetCacheEnable(false);
            var up = DB.NewMetaUnitParameter();
            var s = from t in DB.LamdaTable(up, "EXTEND_ROLE", "a")
                    select t;
            return s.Query(up).QueryData<FrameDLRObject>()
                .Select((p) =>
                {
                    dynamic obj = p;
                    return new
                    {
                        obj.roleno,
                        obj.rolename
                    };
                }).ToList<object>();
        }
        [ActionAuth("Role")]
        [EWRAEmptyValid("role_no,role_name")]
        [EWRARouteDesc("新增一个角色")]
        [EWRAAddInput("role_no", "string", "角色编号", "无默认值", EFFC.Frame.Net.Base.Constants.RestInputPosition.PostData, false)]
        [EWRAAddInput("role_name", "string", "角色名称", "无默认值", EFFC.Frame.Net.Base.Constants.RestInputPosition.PostData, false)]
        [EWRAOutputDesc("返回结果", @"{
code:""success-成功，failed-失败"",
msg:""提示信息"",
role_no:""成功时候返回的角色编号""
}")]
        public override object post()
        {
            BeginTrans();
            var no = ComFunc.nvl(PostDataD.role_no);
            var name = ComFunc.nvl(PostDataD.role_name);

            var up = DB.NewMetaUnitParameter();
            if ((from t in DB.LamdaTable(up, "EXTEND_ROLE", "a")
                 where t.roleno == no || t.rolename == name
                 select t).IsExists(up))
            {
                return new
                {
                    code = "failed",
                    msg = "角色编号/名称已存在"
                };
            }
            DB.LamdaTable(up, "EXTEND_ROLE", "a").Insert(up, new
            {
                roleno = no,
                rolename = name,
                Add_Id = TokenPayLoad.ID,
                Add_IP = ClientInfo.IP,
                Add_Time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                Last_Id = TokenPayLoad.ID,
                Last_IP = ClientInfo.IP,
                Last_Time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
            });
            CommitTrans();
            return new
            {
                code = "success",
                msg = "新增成功",
                role_no = no
            };
        }
        [ActionAuth("Role")]
        [EWRARouteDesc("删除一个角色,但系统管理的角色R999不可被删除")]
        [EWRAAddInput("id", "string", "角色编号", "无默认值", EFFC.Frame.Net.Base.Constants.RestInputPosition.Path, false)]
        [EWRAOutputDesc("返回结果", @"根据http的状态码来识别，204标识操作成功，404表示操作识别未找到删除的资料")]
        public override bool delete(string id)
        {
            BeginTrans();
            var up = DB.NewMetaUnitParameter();
            var s = from t in DB.LamdaTable(up, "EXTEND_ROLE", "a")
                    where t.roleno == id
                    select t;
            if (!s.IsExists(up))
            {
                return false;
            }
            s.Delete(up);
            //删除角色功能映射表
            (from t in DB.LamdaTable(up, "EXTEND_ROLE_FUNCTION", "a")
             where t.roleno == id
             select t).Delete(up);
            //删除角色登录用户映射表
            (from t in DB.LamdaTable(up, "EXTEND_ROLE_LOGIN", "a")
             where t.roleno == id
             select t).Delete(up);
            CommitTrans();
            return true;
        }
        [ActionAuth("Role")]
        [EWRARouteDesc("修改一个角色")]
        [EWRAAddInput("id", "string", "角色编号", "无默认值", EFFC.Frame.Net.Base.Constants.RestInputPosition.Path, false)]
        [EWRAAddInput("role_name", "string", "角色名称", "无默认值", EFFC.Frame.Net.Base.Constants.RestInputPosition.PostData, false)]
        [EWRAOutputDesc("返回结果", @"{
code:""success-成功，failed-失败"",
msg:""提示信息"",
role_no:""成功时候返回的功能编号""
}")]
        public override object patch(string id)
        {
            BeginTrans();
            var up = DB.NewMetaUnitParameter();
            var name = ComFunc.nvl(PostDataD.role_name);

            var s = from t in DB.LamdaTable(up, "EXTEND_ROLE", "a")
                    where t.roleno == id
                    select t;
            if (!s.IsExists(up))
            {
                return new
                {
                    code = "failed",
                    msg = "数据不存在"
                };
            }
            if ((from t in DB.LamdaTable(up, "EXTEND_ROLE", "a")
                 where t.rolename == name && t.roleno != id
                 select t).IsExists(up))
            {
                return new
                {
                    code = "failed",
                    msg = "角色名称重复"
                };
            }
            s.Update(up, new
            {
                RoleName = name,
                Last_Id = TokenPayLoad.ID,
                Last_IP = ClientInfo.IP,
                Last_Time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
            });
            CommitTrans();
            return new
            {
                code = "success",
                msg = "修改成功",
                role_no = id
            };
        }
    }
}
